
const _ = require('lodash');
const moment = require('moment');
const newService = require('../../service/news/news');
const fastStaticsService = require('../../service/news/statistics');

let Fast = {};
//
// /**
//  *  根据指定id获取快讯内容
//  */
// Fast.getFastByIds = (req, res, next)=> {
//     if (!req.query.ids) {
//         return res.error('参数错误', 400)
//     }
//
//     let where = {}, fastInfo = [], fastStatics = {};
//     let ids = req.query.ids.split(',');
//     where._id = {$in: ids};
//     //  拉取所有的快讯
//     return newService.getAllFastInfo({where: where, fields: '-__v -readNum -readNumFalse'}, {lean: true})
//         .then((data)=> {
//             fastInfo = data;
//             let ids = _.map(data, (item)=> item._id);
//             return newService.updateAndSaveFastStatics({_id: ids})
//         })
//         .then((data)=> {
//             fastStatics = data;
//             let authorIds = _.map(fastInfo, (item)=> {
//                 if (item.author.length > 3) {
//                     return item.author;
//                 }
//             });
//             return newService.getFastAuthor(authorIds)
//         })
//         .then((data)=> {
//             let i;
//             for (i = 0; i < fastInfo.length; i++) {
//                 let single = fastInfo[i];
//                 if (fastStatics[single._id]) {
//                     fastInfo[i].viewCount = fastStatics[single._id].viewCount || 0;    //阅读次数
//                     fastInfo[i].topCount = fastStatics[single._id].realTopCount || 0;      //点赞次数
//                     fastInfo[i].collectCount = fastStatics[single._id].realCollectCount || 0;
//                     fastInfo[i].playCount = fastStatics[single._id].playCount || 0;    //我来播次数
//                 } else {
//                     fastInfo[i].viewCount = 1;
//                     fastInfo[i].topCount = 0;
//                     fastInfo[i].collectCount = 0;
//                     fastInfo[i].playCount = 0;
//                 }
//                 //  添加名字
//                 fastInfo[i].author = data[single.author] && data[single.author].userName
//                     || (fastInfo[i].author || '')
//             }
//             //  按照Id传进来的顺序再返回给前端
//             fastInfo = fastInfo.sort((d1, d2)=> {
//                 return ids.indexOf(d1._id.toString()) - ids.indexOf(d2._id.toString())
//             });
//             res.success(fastInfo)
//         })
//         .catch((err)=>{
//             next(err)
//         })
// };

/**
 * 获取快讯列表
 */
Fast.getFastList = (req, res, next)=> {

    //  快讯类型
    let types = req.query.types || '104';
    const allowType = {0: 1, 2: 1, 4: 1, 11: 1, 101: 1, 102: 1, 103: 1, 104: 1};
    if (types) {
        //过滤type的类型
        types = types.split(',').filter((par)=> allowType[par])
    }
    let fastWhere = {type: {$in: types}};
    let now = new moment().subtract((req.query.page || 1) - 1, 'd');
    let limit = req.query.perPage || 100;
    let fastInfo = [], fastStatics = {};
    fastWhere.releaseDate = {$gte: now.startOf('day').valueOf(), $lte: now.endOf('day').valueOf()};

    newService.getAllFastInfo({where: fastWhere, fields: '-__v -readNum'}, {
        lean: true, limit: limit, sort: {releaseDate: -1}
    })
        .then((fast)=> {
            fastInfo = fast;
            let _ids = fast.map(item => item._id);
            if (!_ids || _ids.length === 0) {
                return Promise.resolve()
            }
            //  浏览过的就算读过了，次数加上
            return fastStaticsService.updateAndSaveFastStatics({_ids: _ids})
        })
        .then((resp)=> {
            fastStatics = resp;
            //fixme 这一步超级蠢， 给改掉,用户名称这种东西 就应该冗余存！
            let authorIds = _.map(fastInfo, (item)=> {
                if (item.author.length > 3) {
                    return item.author;
                }
            });
            //获取发表快讯的作者名称
            return newService.getFastAuthor(authorIds)
        })
        .then((data)=> {
            let i;
            for (i = 0; i < fastInfo.length; i++) {
                let single = fastInfo[i];
                if (fastStatics[single._id]) {
                    fastInfo[i].viewCount = fastStatics[single._id].viewCount || 0;    //阅读次数
                    fastInfo[i].topCount = fastStatics[single._id].realTopCount || 0;      //点赞次数
                    fastInfo[i].collectCount = fastStatics[single._id].realCollectCount || 0;
                    fastInfo[i].playCount = fastStatics[single._id].playCount || 0;    //我来播次数
                } else {
                    fastInfo[i].viewCount = 1;
                    fastInfo[i].topCount = 0;
                    fastInfo[i].collectCount = 0;
                    fastInfo[i].playCount = 0;
                }
                //  添加名字
                fastInfo[i].author = data[single.author] && data[single.author].userName
                    || (fastInfo[i].author || '')
            }
            res.success(fastInfo)
        })
        .catch((err)=> {
            next(err)
        })
};

/**
 * 统计快讯相关数据
 */
Fast.statistic = (req, res, next)=> {
    
    let type = [];
};

module.exports = Fast;